﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Chisel.Core
{

	/// <summary>
	/// Handler delegate for the TextChanged event text in a TextField.
	/// </summary>
	public delegate void TextChangedHandler(string text);

	/// <summary>
	/// A text box with some custom functionality.
	/// </summary>
	/// <remarks>Most importantly, it captures mouse input while open and
	/// requests verification for the text change from some sort of model.</remarks>
	public class TextField : UserControl
	{
		/// <summary>
		/// By default, the control is invisible. Use Show() to show it.
		/// </summary>
		public TextField()
			: base()
		{
			_textBox = new TextBox();
			_textBox.LostFocus += new RoutedEventHandler(OnLostFocus);
			_textBox.KeyDown += new KeyEventHandler(OnKeyDown);
			Content = _textBox;

			Visibility = Visibility.Collapsed;
		}


		private TextBox _textBox;

		/// <summary>
		/// This gets raised to find out if the change is okay.
		/// </summary>
		private TextChangedHandler TextChanged;

		/// <summary>
		/// Shows the text box at pos with the given size.
		/// </summary>
		/// <param name="pos">The position of the text box (presumably on a canvas).</param>
		/// <param name="size">The size of the text box.</param>
		/// <param name="initial">The initial text.</param>
		/// <param name="handler">The handler to call when the user wants to change the text.</param>
		public void Show(Point pos, Point size, string initial, TextChangedHandler handler)
		{
			Canvas.SetLeft(this, pos.X);
			Canvas.SetTop(this, pos.Y);
			_textBox.Width = size.X;
			_textBox.Height = size.Y;

			_textBox.Text = initial;

			TextChanged = null;
			TextChanged = handler;

			_textBox.Focus();
			_textBox.SelectAll();

			Visibility = Visibility.Visible;
		}

		/// <summary>
		/// Handles the text box losing focus.
		/// </summary>
		private void OnLostFocus(object sender, RoutedEventArgs e)
		{
			if (Visibility == Visibility.Collapsed)
				return;
			Submit();
		}

		/// <summary>
		/// Handles a key down event in the text box and decides what to do with it.
		/// </summary>
		void OnKeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Escape)
				Cancel();
			else if (e.Key == Key.Enter)
				Submit();
		}

		/// <summary>
		/// Cancels the text entry and hides the control.
		/// </summary>
		public void Cancel()
		{
			Visibility = Visibility.Collapsed;
			_textBox.LostFocus -= OnLostFocus;
			TextChanged = null;
		}

		/// <summary>
		/// Submits the new text value and hide the control.
		/// </summary>
		public void Submit()
		{
			_textBox.LostFocus -= OnLostFocus;
			Visibility = Visibility.Collapsed;
			if (TextChanged != null)
				TextChanged(_textBox.Text);
		}

	}
}
